﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TERP.WaterClient.Common;
using System.Threading;

namespace TERP.WaterClient.Views.Common
{
	public partial class FrmProgressMarquee : XtraForm, WorkerProcessMarqueeListener
	{
        private WorkerProcessMarquee _worker = null;
		private object _processResult = null;

        public FrmProgressMarquee()
		{
			InitializeComponent();
			this.Text = "Đang xử lý...";
		}

		public WorkerProcessMarquee WorkerProcess{
			set { this._worker = value; }
		}

		public object ProcessResult{
			get { return _processResult; }
		}

		private void FrmProgress_Load(object sender, EventArgs e)
		{
            this.marqueeProgressBarControl1.Properties.ProgressViewStyle = DevExpress.XtraEditors.Controls.ProgressViewStyle.Broken;
			Thread workerThread = new Thread(_worker.StartProcess);
			workerThread.Start();
		}

		#region WorkerProcessListener Members

		public void ProccessFinished()
		{
			if (this.InvokeRequired)
			{
				CloseFormCallback d = new CloseFormCallback(CloseForm);
				this.Invoke(d);
			}
			else
			{
				this.CloseForm();
			}
		}

		private delegate void SetLableTextCallback(string text);       
		private delegate void CloseFormCallback();

		private void SetLabelProgress(string text){
			this.lblProgress.Text = text;
		}  

		private void CloseForm()
		{
			this.Close();
		}

		public void SetProgressText(string text)
		{
			if (this.lblProgress.InvokeRequired)
			{
				SetLableTextCallback d = new SetLableTextCallback(SetLabelProgress);
				this.Invoke(d, new object[] { text});
			}
			else
			{
				this.SetLabelProgress(text);
			}
		}

		private void btnStop_Click(object sender, EventArgs e)
		{
			this.btnStop.Enabled = false;
			_worker.Interupt();
		}


		public void SetProcessResult(object result)
		{
			this._processResult = result;
		}

		#endregion
	}
}
